﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Money.Data
{
    public partial class User
    {
        public List<Role> Roles
        {
            get
            {
                var query = from uir in this.UserInRoles
                            where uir.UserId == this.UserId
                            select uir.Role;
                return query.ToList<Role>();
            }
            set
            {
                this.UserInRoles.Clear();
                foreach (Role item in value)
                {
                    UserInRole uir = new UserInRole();
                    uir.UserId = this.UserId;
                    uir.RoleId = item.RoleId;

                    this.UserInRoles.Add(uir);
                }
            }
        }

        public bool IsInRole(RoleType roleType)
        {
            foreach (Role item in this.Roles)
            {
                if (item.RoleId == (int)roleType)
                    return true;
            }
            return false;
        }
    }
}
